async function initWebGPU(canvas: HTMLCanvasElement) {
  if (!navigator.gpu) {
    throw new Error('WebGPU not supported');
  }
  const adapter = await navigator.gpu.requestAdapter({
    powerPreference: 'high-performance'
  });
  if (!adapter) {
    throw new Error('no adapter available');
  }
  const device = await adapter.requestDevice();
  const context = canvas.getContext('webgpu');
  if (!context) {
    throw new Error('no context available');
  }
  canvas.width = canvas.clientWidth * window.devicePixelRatio;
  canvas.height = canvas.clientHeight * window.devicePixelRatio;
  const size = {
    width: canvas.width,
    height: canvas.height,
  };
  const format = navigator.gpu.getPreferredCanvasFormat();
  context.configure({
    device,
    format,
  })
  return { adapter, device, format, context, size };
}

export { initWebGPU }